package cn.db101.jcc.service;

import cn.db101.jcc.entity.Favorites;
import cn.db101.jcc.entity.Lineup;
import cn.db101.jcc.mapper.FavoritesMapper;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static cn.db101.jcc.entity.table.Tables.FAVORITES;

/**
* @author win
* @description 针对表【t_favorites(收藏表)】的数据库操作Service实现
* @createDate 2024-06-12 01:33:09
*/
@Service
public class FavoritesService {

    @Autowired
    private LineupService lineupService;

    @Autowired
    private FavoritesMapper favoritesMapper;

    /**
     * 通过用户查询收藏列表
     * @param userId
     * @return
     */
    public List<Lineup> lineUpList(int userId) {
        // 查询收藏的Id
        List<Favorites> favoritesList = favoritesMapper.selectListByQuery(QueryWrapper.create()
                .select(Favorites::getLineupId)
                .from(Favorites.class)
                .where(FAVORITES.USER_ID.eq(userId)));

        return lineupService.listFromId(favoritesList.stream().map(Favorites::getLineupId).collect(Collectors.toList()));
    }

    /**
     * 删除收藏
     * @param favorites
     */
    public void deleteFavorites(Favorites favorites) {

        favoritesMapper.deleteByQuery(QueryWrapper.create()
                .where(FAVORITES.USER_ID.eq(favorites.getUserId()))
                .and(FAVORITES.LINEUP_ID.eq(favorites.getLineupId())));
    }

    public Favorites selectOne(Favorites favorites) {

        return favoritesMapper.selectOneByQuery(QueryWrapper.create()
                .where(FAVORITES.USER_ID.eq(favorites.getUserId()))
                .and(FAVORITES.LINEUP_ID.eq(favorites.getLineupId())));
    }

    public void save(Favorites favorites) {

        favoritesMapper.insert(favorites);
    }

}




